package com.yunji.web.order;

import com.yunji.common.annotation.Log;
import com.yunji.common.annotation.RepeatSubmit;
import com.yunji.common.base.BaseController;
import com.yunji.common.base.response.Response;
import com.yunji.common.base.response.ResponseHelper;
import com.yunji.common.enums.common.BusinessType;
import com.yunji.common.utils.ExcelUtil;
import com.yunji.common.utils.TimeUtils;
import com.yunji.order.business.PayoutBusiness;
import com.yunji.order.command.order.OrderStateMachine;
import com.yunji.order.domain.OrderDO;
import com.yunji.order.domain.PayoutDO;
import com.yunji.order.dto.PayoutPageDTO;
import com.yunji.order.dto.PayoutCreateDTO;
import com.yunji.order.dto.cmd.OrderPaidCommandDTO;
import com.yunji.order.emuns.OrderStatusEnum;
import com.yunji.order.wechat.WechatCallbackDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @Package com.yunji.web.order
 * FileName:	PayoutController.java
 * Copyright:	Copyright © 2024 云集互联 All Rights Reserved
 * Company:		云集互联
 * @version: 1.0
 * @since: JDK 1.8.0_202
 * Create at:   2024-04-21 19:40:16
 * Description:
 * 用户提现
 * Modification History:
 * Author					Date					Version         Description
 * -------------------------------------------------------------------------------------
 * HuaAo0803@163.com	2024-04-21 19:40:16     1.0             1.0 Version
 */
@Api(tags = "管理端-提现模块")
@RestController
@RequestMapping("/container/payout/")
public class PayoutController extends BaseController {
    @Resource
    private PayoutBusiness payoutBusiness;


    @ApiOperation("模拟支付宝回调(前端不用关注)")
    @GetMapping("/testPayoutCallBack/{payoutCode}")
    public Response<?> testPayCallBack(@PathVariable("payoutCode") String payoutCode) {
        String payCode = "peter_test";
        payoutBusiness.callback(payoutCode,payCode,"2025-04-16 11:11:11");
        return ResponseHelper.success();
    }


    @ApiOperation("查询提现列表")
    @GetMapping("/list")
    public Response<List<PayoutDO>> list(@Valid PayoutPageDTO payout) {
        List<PayoutDO> list = payoutBusiness.searchPayout(payout);
        return ResponseHelper.successWithPage(list);
    }

    @ApiOperation("导出提现信息")
    @Log(title = "提现", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('transaction:payouts:export')")
    @PostMapping("/export")
    public void export(PayoutPageDTO payout) {
        payout.setExport(true);
        List<PayoutDO> list = payoutBusiness.searchPayout(payout);
        ExcelUtil<PayoutDO> util = new ExcelUtil<>(PayoutDO.class);
        util.exportExcel(response, list, "提现数据");
    }

    @ApiOperation("获取提现详细信息")
    @GetMapping(value = "/{id}")
    public Response<PayoutDO> getInfo(@PathVariable("id") Long id) {
        return ResponseHelper.success(payoutBusiness.selectPayout(id));
    }

    @ApiOperation("添加提现申请")
    @PostMapping(value = "/addPayout")
    @RepeatSubmit
    public Response<Long> create(@Valid @RequestBody PayoutCreateDTO payout) {
        return ResponseHelper.success(payoutBusiness.createPayout(payout));
    }

    @ApiOperation("修改提现")
    @Log(title = "提现", businessType = BusinessType.UPDATE)
    @PutMapping
    public Response<String> update(@RequestBody PayoutDO payout) {
        payoutBusiness.updatePayout(payout);
        return ResponseHelper.success();
    }

    @ApiOperation("关闭提现申请")
    @PostMapping(value = "/closed")
    public Response<PayoutDO> closed(Long id) {
        payoutBusiness.closedPayout(id);
        return ResponseHelper.success();
    }

    @ApiOperation("删除提现")
    @DeleteMapping("/{ids}")
    public Response<String> remove(@PathVariable Long[] ids) {
        payoutBusiness.deletePayout(ids);
        return ResponseHelper.success();
    }

    @ApiOperation("同意提现")
    @RepeatSubmit
    @GetMapping("/agreePayout")
    public Response<Object> agreePayout(@RequestParam(name = "payoutId") Long payoutId) {
        return ResponseHelper.handle(payoutBusiness.auditPayout(payoutId),"同意提现失败");
    }

    @ApiOperation("拒绝提现")
    @RepeatSubmit
    @GetMapping("/rejectRefund")
    public Response<Object> rejectRefund(@RequestParam(name = "payoutId") Long payoutId, @RequestParam(name = "reason") String reason) {
        return ResponseHelper.handle(payoutBusiness.rejectRefund(payoutId, reason),"拒绝提现失败");
    }

    /**
     * ------------------------------------------ 搜索栏 ------------------------------------------
     */

    @ApiOperation("申请人姓名匹配搜索")
    @GetMapping("/searchApplicantName")
    public Response<List<String>> searchApplicantName(@RequestParam("applicantName") String applyName) {
        return ResponseHelper.successWithPage(payoutBusiness.searchApplicantName(applyName));
    }

    @ApiOperation("申请人手机号匹配搜索")
    @GetMapping("/searchApplicantPhone")
    public Response<List<String>> searchApplicantPhone(@RequestParam("applicantPhone") String applyPhone) {
        return ResponseHelper.successWithPage(payoutBusiness.searchApplicantPhone(applyPhone));
    }
}